package com.polaris.live.viewmodel.home

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.blankj.utilcode.util.CollectionUtils
import com.polaris.live.common.constant.CommonConst
import com.polaris.live.common.manager.withRetry
import com.polaris.live.common.mvvm.viewmodel.BaseViewModel
import com.polaris.live.common.network.request
import com.polaris.live.common.network.state.ResultState
import com.polaris.live.net.homeService
import com.polaris.live.net.liveService
import com.polaris.live.resp.back_resp.ListWithBannerBean
import com.polaris.live.resp.back_resp.PartyGameResp
import com.polaris.live.resp.back_resp.PartyRoomResp
import com.polaris.live.resp.base.ListResp
import com.polaris.live.resp.base.new
import com.polaris.live.utils.CommonUtils
import com.polaris.live.widget.refresh.OnListListener
import kotlinx.coroutines.async
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.launch

/**
 * HomeGameViewModel
 * @author Created by 天晴 on 2024/1/18 19:09
 * @since 1.0.0
 **/
class HomeGameViewModel : BaseViewModel(), OnListListener {

    /**
     * 获取派对游戏
     */
    val mPartyGameLiveData = MutableLiveData<ResultState<List<PartyGameResp>>>()
    fun getPartyGame() {
        viewModelScope.launch {
            withRetry(Int.MAX_VALUE, { 5000L },
                {
                    request({ liveService.getPartyGame() }, mPartyGameLiveData)
                }
            )
        }
    }

    var gameId: Int? = null

    val mListLiveData = MutableLiveData<ResultState<ListResp<Any>>>()
    private fun getGamePage(position: Int) {
        request({
            if (position == 0) {
                coroutineScope {
                    val listResp = async { homeService.getGamePage(position, filterGameType = gameId) }.await()

                    val newList =
                        CommonUtils.extGameDealWith(listResp.ext, listResp.result?.list ?: emptyList()).toMutableList()

                    val bannerResp =
                        async { homeService.getBannerListV2(CommonConst.BannerType.BANNER_PARTY_GAME) }.await()

                    if (CollectionUtils.isNotEmpty(bannerResp.result?.banner)) {
                        newList.add(0, ListWithBannerBean(bannerResp.result?.banner ?: emptyList()))
                    }

                    listResp.new(newList)
                }
            } else {
                homeService.getGamePage(position).let {
                    val newList =
                        CommonUtils.extGameDealWith(it.ext, it.result?.list ?: emptyList()).toMutableList()
                    it.new(newList)
                }
            }
        }, mListLiveData)
    }

    override fun onRefresh() {
        getGamePage(0)
    }

    override fun onLoadMore(pageIndex: Int) {
        getGamePage(pageIndex)
    }

    val mPartyRoomLiveData = MutableLiveData<ResultState<PartyRoomResp>>()
    fun getPartyRoom() {
        request({ liveService.getPartyRoom() }, mPartyRoomLiveData)
    }
}